package com.cl.interceptor;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSONObject;
import com.cl.util.Constant;

/**
 * 权限拦截器
 * @author chen
 * @date 2016-9-28 下午1:24:13
 */
public class AuthInterceptor extends HandlerInterceptorAdapter{
	
	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		HandlerMethod method = (HandlerMethod)handler;
		Auth auth = method.getMethod().getAnnotation(Auth.class);
		if(auth != null && auth.verifyLoginM()){//管理后台登录验证
			HttpSession session = request.getSession();
			if(session.getAttribute(Constant.SESSION_USER_M)==null){//未登录
				//判断ajax请求
				if("XMLHttpRequest".equals(request.getHeader("x-requested-with"))){
					JSONObject json = new JSONObject();
					json.put("success", false);
					json.put("type", Constant.MSG_TYPE_SESSION);
					json.put("msg", Constant.MSG_SESSION_M);
					PrintWriter printWriter;
					try {
						response.setContentType("json;charset=utf-8");
						printWriter = response.getWriter();
						printWriter.print(json.toString()); 
						printWriter.flush();  
		                printWriter.close();
					} catch (IOException e) {
						e.printStackTrace();
					}  
					return false;
				}
				String param = "";
				if(request.getQueryString() != null){
					param = "?"+request.getQueryString();
				}
				session.setAttribute("returnUrl", request.getRequestURL().toString()+param);
				response.sendRedirect(request.getContextPath());
				return false;
			}else{
				session.removeAttribute("returnUrl");
			}
		}else if(auth != null && auth.verifyLoginC()){//前端登录验证
			HttpSession session = request.getSession();
			if(session.getAttribute(Constant.SESSION_USER_C)==null){//未登录
				//判断ajax请求
				if("XMLHttpRequest".equals(request.getHeader("x-requested-with"))){
					JSONObject json = new JSONObject();
					json.put("success", false);
					json.put("type", Constant.MSG_TYPE_SESSION);
					json.put("msg", Constant.MSG_SESSION_C);
					PrintWriter printWriter;
					try {
						response.setContentType("json;charset=utf-8");
						printWriter = response.getWriter();
						printWriter.print(json.toString()); 
						printWriter.flush();  
		                printWriter.close();
					} catch (IOException e) {
						e.printStackTrace();
					}  
					return false;
				}String param = "";
				if(request.getQueryString() != null){
					param = "?"+request.getQueryString();
				}
				session.setAttribute("returnUrl", request.getRequestURL().toString()+param);
				response.sendRedirect(request.getContextPath()+"/appLogin.jsp");
				return false;
			}else{
				session.removeAttribute("returnUrl");
			}
		}
		return super.preHandle(request, response, handler);
	}
	
	
}
